package com.laogao.controller;

import com.laogao.common.AjaxResult;
import com.laogao.common.page.PageResult;
import com.laogao.entity.TDocument;
import com.laogao.service.AdxDocumentService;
import com.laogao.service.impl.FileServiceImpl;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;

/**
 * @author gao liang
 * @date 2023-03-07 10:01
 */
@RestController
@Tag(name = "文件管理")
@RequiredArgsConstructor
public class DocumentController {

    private final AdxDocumentService adxDocumentService;
    private final FileServiceImpl fileService;

    @Operation(summary = "查询")
    @Parameter(name = "id", description = "id", required = true)
    @GetMapping(path = "/document", produces = {"application/json;charset=UTF-8"})
    public AjaxResult query(@RequestParam String id) {
        TDocument tDocument = adxDocumentService.query(Long.parseLong(id));
        return AjaxResult.success(tDocument);
    }

    @Operation(summary = "添加")
    @PostMapping(path = "/add")
    public AjaxResult add(MultipartFile file, HttpServletRequest request) throws Exception {
        adxDocumentService.add(file, request);
        return AjaxResult.success();
    }

    @Operation(summary = "修改")
    @PostMapping(path = "/edit")
    public AjaxResult update(@RequestParam("id") Long id, @RequestParam(required = false) MultipartFile file, HttpServletRequest request) throws Exception {
        adxDocumentService.update(id, file, request);
        return AjaxResult.success();
    }

    @Operation(summary = "删除")
    @Parameter(name = "id", description = "id", required = true)
    @DeleteMapping(path = "/delete")
    public AjaxResult delete(@RequestParam(value = "id") String id) throws Exception {
        adxDocumentService.delete(Long.parseLong(id));
        return AjaxResult.success();
    }

    @Operation(summary = "通用文件上传(别名下载)")
    @PostMapping(path = "/uploadFileForNickName")
    public AjaxResult uploadFileNickName(MultipartFile file, HttpServletRequest request) throws IOException {
        HashMap<String, Object> info = fileService.uploadFileNickName(file, request);
        return AjaxResult.success(info);
    }

    @Operation(summary = "通用文件上传(原名下载)")
    @PostMapping(path = "/uploadFileForOriginalName")
    public AjaxResult uploadFileOriginalName(MultipartFile file, HttpServletRequest request) throws IOException {
        HashMap<String, Object> info = fileService.uploadFileOriginalName(file, request);
        return AjaxResult.success(info);
    }

    /**
     * http://localhost:8998/api/sys-cls/download?ids=1,2,3
     *
     * @param ids
     * @param response
     * @throws IOException
     */
    @Operation(summary = "文件下载", description = "id为一个直接下载文件,id为多个时以压缩包形式下载")
    @GetMapping(path = "/download")
    public void download(@RequestParam List<Long> ids, HttpServletResponse response) throws Exception {
        fileService.download(ids, response);
    }

    @Operation(summary = "分页展示")
    @Parameters(
            {@Parameter(name = "currentPage", description = "页码"),
                    @Parameter(name = "pageSize", description = "每页条数"),
                    @Parameter(name = "type", description = "分类 1:图片,按项目2:音频 3:视频 4:文档", required = true),
                    @Parameter(name = "fileName", description = "文件名称", required = false)
            })
    @GetMapping(path = "/page", produces = {"application/json;charset=UTF-8"})
    public AjaxResult clsModelTreePage(@RequestParam(value = "currentPage", defaultValue = "1") Integer currentPage,
                                       @RequestParam(value = "pageSize", defaultValue = "100") Integer pageSize,
                                       @RequestParam(value = "type", defaultValue = "1") Integer type,
                                       @RequestParam(value = "fileName", required = false) String fileName) throws Exception {
        PageResult<TDocument> page = adxDocumentService.getPage(currentPage, pageSize, type, fileName);
        return AjaxResult.success(page);
    }
}
